package com.wfk.h5.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wfk.entity.Customer;
import com.wfk.entity.MoneyDetails;
import com.wfk.entity.ReceivedAddr;
import com.wfk.entity.SysCashDetails;
import com.wfk.h5.mapping.MappingUtils;
import com.wfk.h5.service.MemberService;
import com.wfk.service.IAccountService;
import com.wfk.service.IAddressService;
import com.wfk.service.IAppBannersService;
import com.wfk.service.IBankDetailsService;
import com.wfk.service.IBaseInfoService;
import com.wfk.service.ICustomerService;
import com.wfk.service.IGoodsService;
import com.wfk.service.IMoneyDetailsService;
import com.wfk.service.IReceivedAddrService;
import com.wfk.service.ISysCashDetailsService;
import com.wfk.service.ISysNewsService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ResponseData;
import com.wfk.vo.MemberVo;

@Controller
public class MemberController {
	@Autowired
	private MemberService memberService;
	@Autowired
	private IBankDetailsService bankDetailsService;
	@Autowired
	private IMoneyDetailsService moneyDetailsService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private IReceivedAddrService receivedAddrService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private ISysCashDetailsService sysCashDetailsService;
	@Autowired
	private IAppBannersService  appBannersService;
	@Autowired
	private ISysNewsService sysNewsService;
	@Autowired
	private IGoodsService goodsService;
	@Autowired
	private IAccountService accountService;
	@Autowired
	private IBaseInfoService baseInfoService;
	
	@RequestMapping(MappingUtils.userInfo)
	@ResponseBody
	public ResponseData userInfo(Integer cid) {
		ResponseData result = ResponseData.ok(); 
		Customer cus = this.customerService.getById(cid);
		if(cus!=null) {
			result.putDataValue("realname", cus.getRealname());
			result.putDataValue("headurl", cus.getHeadurl());
			result.putDataValue("phone", cus.getPhone());
			result.putDataValue("qrurl", cus.getQrurl());
			result.putDataValue("shareUser", baseInfoService.getByKey("appurl")+"/shareUser?tjphone="+cus.getId());
		}
		return result;
	}
	
	@RequestMapping(MappingUtils.home)
	@ResponseBody
	public ResponseData home() {
		try {
			ResponseData result = ResponseData.ok();
			result.putDataValue("banner",appBannersService.list(null));
			result.putDataValue("news", sysNewsService.list(null));
			result.putDataValue("goods", goodsService.list(null));
			return result;
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.handleCash)
	@ResponseBody
	public ResponseData doCash(Integer bankid,BigDecimal cashmoney,Integer cid,String payword) {
		try {
			SysCashDetails cashDetail =  new SysCashDetails();
			cashDetail.setBankid(bankid);
			cashDetail.setCashmoney(cashmoney);
			cashDetail.setTargetid(cid);
			cashDetail.setPayword(payword);
			return sysCashDetailsService.doCash(cashDetail);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.cashRule)
	@ResponseBody
	public ResponseData cashRule(Integer cid) {
		try {
			return memberService.cashRule(cid);
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	/**
	 * 我的团队
	 * @param page
	 * @param cid
	 * @return
	 */
	@RequestMapping(value=MappingUtils.myTeam)
	@ResponseBody
	public ResponseData myTeam(BasePage<MemberVo> page,MemberVo vo){
		try{
			return ResponseData.ok().putDataValue("result",this.customerService.myTeam(page, vo));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	/**
	 * 删除地址
	 * @param cid
	 * @param id
	 * @return
	 */
	@RequestMapping(value=MappingUtils.devAddress)
	@ResponseBody
	public ResponseData devAddress(Integer cid,Integer id){
		try{
			this.receivedAddrService.remove(new QueryWrapper<ReceivedAddr>().eq("id", id).eq("cid",cid));
			return ResponseData.ok();
		}catch(Exception e){
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	/**
	 * 保存新地址
	 * @param cid
	 * @param name
	 * @param phone
	 * @param proid
	 * @param cityid
	 * @param areaid
	 * @param address
	 * @param isdefault
	 * @return
	 */
	@RequestMapping(value=MappingUtils.addRessUrl)
	@ResponseBody
	public ResponseData saveAddress(Integer id,Integer cid,String name,String phone,Integer proid,Integer cityid,Integer areaid
			,String address,Integer isdefault){
		try{
			return this.memberService.saveReceivedAddr(id,cid, name, phone, proid, cityid, areaid, address, isdefault);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	/**
	 * 默认地址
	 * @param cid
	 * @param id
	 * @return
	 */
	@RequestMapping(MappingUtils.addressDefault)
	@ResponseBody
	public ResponseData upDefault(Integer cid,Integer id){
		try{
			return this.memberService.upDefault(cid, id);
		}catch(Exception e){
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	/**
	 * 根据pid查询地址
	 * @param pid
	 * @return
	 */
	@RequestMapping(value = MappingUtils.addressList)
	@ResponseBody
	public ResponseData queryAddressByPid(Integer cid) {
		try{
			return ResponseData.ok().putDataValue("result",this.receivedAddrService.list(new QueryWrapper<ReceivedAddr>().eq("cid",cid)));
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("内部错误");
		}
	}
	/**
	 * 用户修改密码
	 * @param phone
	 * @param password
	 * @param validcode
	 * @return
	 */
	@RequestMapping(value=MappingUtils.updatePassword)
	@ResponseBody
	public ResponseData password(String phone,String password,String validcode){
		try{
			return memberService.updatePassword(phone, password, validcode);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	

	/**
	 * 用户修改支付密码
	 * @param cid
	 * @param phone
	 * @param passwode
	 * @param validcode
	 * @return
	 */
	@RequestMapping(MappingUtils.updatePayword)
	@ResponseBody
	public ResponseData payword(Integer cid,String payword,String validcode){
		try{
			return memberService.updatePayword(cid,payword,validcode);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.updateName)
	@ResponseBody
	public ResponseData updateName(Integer cid,String headurl,String realname){
		try{
			return this.memberService.updateName(cid,headurl,realname);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误");
		}
	}
	
	/**
	 * 
	 * 绑定银行卡
	 * @param cid
	 * @param realname
	 * @param idcard
	 * @param bankcode
	 * @param typeid
	 * @param phone
	 * @return
	 */
	@RequestMapping(MappingUtils.bindBank)
	@ResponseBody
	public ResponseData saveBank(Integer cid,String realname,String idcard,String bankcode,Integer typeid,String phone){
		try{
			return this.memberService.saveBank(cid, realname, idcard, bankcode, typeid, phone);
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	/**
	 * 我的银行卡列表
	 * @param cid
	 * @return
	 */
	@RequestMapping(MappingUtils.bindBankDev)
	@ResponseBody
	public ResponseData bindBankDev(Integer id){
		try{
			this.bankDetailsService.removeById(id);
			return ResponseData.ok();
		}catch(Exception e){
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	/**
	 * 我的银行卡列表
	 * @param cid
	 * @return
	 */
	@RequestMapping(MappingUtils.mybankdetail)
	@ResponseBody
	public ResponseData myBankDetail(Integer cid){
		try{
			return ResponseData.ok().putDataValue("result",this.bankDetailsService.myBankDetail(cid));
		}catch(Exception e){
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	/**
	 * 余额明细
	 * @param cid
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(MappingUtils.moneydetail)
	@ResponseBody
	public ResponseData showMoneyDetail(MoneyDetails moneyDetails,BasePage<MoneyDetails> page){
		try{
			ResponseData result = ResponseData.ok();
			if(moneyDetails.getCid()==null) {
				return ResponseData.baseServiceError("会员未注册");
			}
			this.moneyDetailsService.listPage(moneyDetails,page);
			result.putDataValue("result",page);
			result.putDataValue("money",this.accountService.get(moneyDetails.getCid()).getMoney());
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return ResponseData.baseServiceError("网络连接错误，请重试");
		}
	}
	
	@RequestMapping(MappingUtils.isPayword)
	@ResponseBody
	public ResponseData isPayword(Integer cid){
		try {
			return ResponseData.ok().putDataValue("result", this.memberService.isPayword(cid));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.badRequest();
		}
	}
	
	@RequestMapping(MappingUtils.isBankDetail)
	@ResponseBody
	public ResponseData isBankDetail(Integer cid){
		try {
			return ResponseData.ok().putDataValue("result", this.memberService.isBankDetail(cid));
		}catch(Exception e) {
			e.printStackTrace();
			return ResponseData.badRequest();
		}
	}
}
